package com.cwj.we.module.activity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.LinearLayout;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.cwj.we.R;
import com.cwj.we.base.BaseActivity;
import com.cwj.we.base.BasePresenter;
import com.cwj.we.base.BaseRVAdapter;
import com.cwj.we.base.BaseRVHolder;
import com.cwj.we.bean.TuijianBean;
import com.cwj.we.utils.ToastUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.gyf.immersionbar.ImmersionBar;

import java.lang.reflect.Type;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.leancloud.LCObject;
import cn.leancloud.LCQuery;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * 推荐网站
 */
public class TuijianUrlActivity extends BaseActivity {

    @BindView(R.id.rvTuijian)
    RecyclerView rvTuijian;
    private BaseRVAdapter<TuijianBean> adapter;

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_tuijian_url;
    }

    @Override
    public void initData() {
        LCQuery<LCObject> query = new LCQuery<>("Tool");
        query.getInBackground("65a8a01f7c88630c72653594").subscribe(new Observer<LCObject>() {
            public void onSubscribe(Disposable disposable) {

            }

            public void onNext(LCObject todo) {
                String tool = todo.getString("tool");
                if (tool != null && !"".equals(tool)) {
                    Type type = new TypeToken<List<TuijianBean>>() {
                    }.getType();
                    List<TuijianBean> list = new Gson().fromJson(tool, type);

                    adapter.setNewData(list);
                } else {
                    ToastUtil.showTextToast(TuijianUrlActivity.this, "暂无推荐");
                }
            }

            public void onError(Throwable throwable) {
                ToastUtil.showTextToast(TuijianUrlActivity.this, throwable.getMessage());
            }

            public void onComplete() {
            }
        });

        adapter = new BaseRVAdapter<TuijianBean>(R.layout.item_tuijian) {
            @Override
            public void onBindVH(BaseRVHolder holder, TuijianBean data, int position) {
                RecyclerView rv = holder.getView(R.id.rv);
                holder.setText(R.id.tvName, data.getType());

                //嵌套子列表
                BaseRVAdapter<TuijianBean.ListBean> listBeanBaseRVAdapter = new BaseRVAdapter<TuijianBean.ListBean>(R.layout.item_tuijian_child) {
                    @Override
                    public void onBindVH(BaseRVHolder holder, TuijianBean.ListBean data, int position) {
                        LinearLayout ll = holder.getView(R.id.ll);
                        holder.setText(R.id.tvName, data.getName());
                        ll.setOnClickListener(v -> {
                            Intent intent = new Intent(TuijianUrlActivity.this, WebViewActivity.class);
                            intent.putExtra("url", data.getUrl());
                            startActivity(intent);
                        });
                    }
                };
                rv.setAdapter(listBeanBaseRVAdapter);
                rv.setLayoutManager(new GridLayoutManager(TuijianUrlActivity.this, 2));//列数设置
                listBeanBaseRVAdapter.setNewData(data.getList());
            }
        };
        rvTuijian.setAdapter(adapter);
        rvTuijian.setLayoutManager(new GridLayoutManager(this, 1));//列数设置
    }

    @Override
    public void initView() {
        ImmersionBar.with(this)
                .statusBarDarkFont(true)  //状态栏字体是深色，不写默认为亮色
                .init();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
    }
}